<template>
    <div class="spare-subclass-page">
        <el-form :inline="true" :model="subclassForm" class="demo-form-inline" style="border-bottom:1px solid #ccc;">
            <!-- :inline默认展示  :model对象，用来存取数据-->
            <!--备件小类编码-->
            <el-form-item :label="nowLangue.spareSubClass+nowLangue.code" class="selectMB10">
                <el-input size="mini" v-model="subclassForm.code" :placeholder="nowLangue.pleaseImport+nowLangue.spareSubClass+nowLangue.code"></el-input>
            </el-form-item>
            <!--备件小类名称-->
            <el-form-item :label="nowLangue.spareSubClass+nowLangue.name" class="selectMB10">
                <el-input size="mini" v-model="subclassForm.name" :placeholder="nowLangue.pleaseImport+nowLangue.spareSubClass+nowLangue.name"></el-input>
            </el-form-item>
            <!--当前状态-->
             <el-form-item :label="nowLangue.currentState" class="selectMB10">
                <el-select size="mini" clearable v-model="subclassForm.status" :placeholder="nowLangue.pleaseSelect" style="width:90px;" >
                    <el-option v-for="(status,index) in statusLists" :key="index" :label="status.label" :value="status.value"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item class="selectMB10">
                <!--查询-->
                <el-button class="bolder outsideBtnBS selectAll" :disabled="selectDisabled" size="mini" type="danger" @click="querySubclssSheet()" v-text="nowLangue.query"></el-button>
            </el-form-item>
        </el-form>
        <!--表格-->
        <!--备件小类明细：-->
        <label class="default-label" v-text="nowLangue.spareSubClass+nowLangue.detailed"></label>
        <!--新增按钮-->
        <!--新增-->
        <el-button class="bolder outsideBtnBS" size="mini" type="danger" @click="createNew()" v-text="nowLangue.create"></el-button>

        <el-table :data="subclassData" border style="width: 100%;" v-loading.body="loading" element-loading-text="拼命加载中">
            <!--序-->
            <el-table-column type="index" header-align="center" align="center" :label="nowLangue.sequence"  width="40"></el-table-column>
            <!--操作-->
            <el-table-column :label="nowLangue.operate" header-align="center" align="center" width="80">
                <template scope="scope">
                    <!--禁用-->
                    <div class="operationItem bolder" v-if="scope.row.status==true" @click="handleStatus(scope.$index,scope.row)" v-text="nowLangue.forbidden"></div>
                    <!--启用-->
                    <div class="operationItem bolder" v-else @click="handleStatus(scope.$index,scope.row)" v-text="nowLangue.enable"></div>
                    <!--修改-->
                    <div class="operationItem disabled bolder" v-if="scope.row.status==true" v-text="nowLangue.update"></div>
                    <!--修改-->
                    <div class="operationItem bolder" v-else @click="updateCorFact(scope.$index,scope.row)" v-text="nowLangue.update"></div>
                </template>
            </el-table-column>
            <!--备件小类编码-->
            <el-table-column prop="code" :label="nowLangue.spareSubClass+nowLangue.code" header-align="center" width="100" show-overflow-tooltip></el-table-column>
            <!--备件小类名称-->
            <el-table-column prop="name" :label="nowLangue.spareSubClass+nowLangue.name" header-align="center" show-overflow-tooltip></el-table-column>
            <!--当前状态-->
            <el-table-column prop="statusName" :label="nowLangue.currStatus" header-align="center" align="center" width="80" show-overflow-tooltip></el-table-column>
            <!--描述-->
            <el-table-column prop="remark" :label="nowLangue.description" header-align="center" show-overflow-tooltip></el-table-column>
            <el-table-column prop="version" :label="nowLangue.description" header-align="center" show-overflow-tooltip></el-table-column>
        </el-table>
        <!--分页-->
        <el-pagination v-if="isPaging" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageIndex"  :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :page-count="totalPage" :total="totalNumber"></el-pagination>
         <!--新建弹窗-->
        <!--新增备件小类信息-->
        <el-dialog :title="nowLangue.create+nowLangue.spareSubClass" :visible.sync="dialogForm1Visible" size="tiny" :lock-scroll="false">
            <el-form :inline="true" :model="addSpareSubclassForm" class="demo-form-inline label-minW120" style="border-bottom:1px solid #ccc;" ref="addSpareSubclassForm">
                <!--备件小类编码-->
                <el-form-item :label="nowLangue.spareSubClass+nowLangue.code" prop="code"  :rules="[{ required: true, message: nowLangue.required},{ max: 20, message: nowLangue.lessThan20}]" >
                    <el-input  v-model.trim="addSpareSubclassForm.code" :placeholder="nowLangue.pleaseImport+nowLangue.spareSubClass+nowLangue.code"></el-input>
                </el-form-item>
                <!--备件小类名称-->
                <el-form-item :label="nowLangue.spareSubClass+nowLangue.name" prop="name"  :rules="[{ required: true, message: nowLangue.required},{ max: 200, message: nowLangue.lessThan200}]" >
                    <el-input  v-model.trim="addSpareSubclassForm.name" :placeholder="nowLangue.pleaseImport+nowLangue.spareSubClass+nowLangue.name"></el-input>
                </el-form-item>
                <!--备件小类描述-->
                <el-form-item :label="nowLangue.spareSubClass+nowLangue.description" >
                    <el-input  v-model="addSpareSubclassForm.remark" :placeholder="nowLangue.pleaseImport+nowLangue.spareSubClass+nowLangue.description"></el-input>
                </el-form-item>
                <!--状态-->
                <el-form-item :label="nowLangue.status" >
                    <el-radio-group v-model="addSpareSubclassForm.status">
                        <!-- 启用 -->
                        <el-radio :label="true" v-cloak>{{nowLangue.enable}}</el-radio>
                        <!-- 禁用 -->
                        <el-radio :label="false" v-cloak>{{nowLangue.forbidden}}</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <!--保存-->
                <el-button class="bolder outsideBtnBS" size="mini" type="danger" @click="saveSpareSubclass" v-text="nowLangue.save" :disabled="saveSSDisabled"></el-button>
                <!--取消-->
                <el-button class="bolder outsideBtnBS" size="mini" type="danger" @click="cancelSpareSubclass" v-text="nowLangue.cancel"></el-button>
            </div>
        </el-dialog>
        <!--修改弹窗-->
        <!--修改备件小类信息-->
        <el-dialog :title="nowLangue.update+nowLangue.spareSubClass" :visible.sync="dialogForm2Visible" size="tiny" :lock-scroll="false">
            <el-form :inline="true" :model="updateSpareSubclassForm" class="demo-form-inline label-minW120" style="border-bottom:1px solid #ccc;" ref="updateSpareSubclassForm">
                <!--备件小类编码-->
                <el-form-item :label="nowLangue.spareSubClass+nowLangue.code" prop="code"  :rules="[{ required: true, message: nowLangue.required},{ max: 20, message: nowLangue.lessThan20}]" >
                    <el-input  v-model.trim="updateSpareSubclassForm.code" disabled></el-input>
                </el-form-item>
                <!--备件小类名称-->
                <el-form-item :label="nowLangue.spareSubClass+nowLangue.name" prop="name"  :rules="[{ required: true, message: nowLangue.required},{ max: 200, message: nowLangue.lessThan200}]" >
                    <el-input  v-model.trim="updateSpareSubclassForm.name" :title="updateSpareSubclassForm.name"></el-input>
                </el-form-item>
                <!--备件小类描述-->
                <el-form-item :label="nowLangue.spareSubClass+nowLangue.description" >
                    <el-input  v-model="updateSpareSubclassForm.remark" :placeholder="nowLangue.pleaseImport+nowLangue.spareSubClass+nowLangue.description"></el-input>
                </el-form-item>
                <!--状态-->
                <el-form-item :label="nowLangue.status" >
                    <el-radio-group v-model="updateSpareSubclassForm.status">
                        <!-- 启用 -->
                        <el-radio :label="true" v-cloak>{{nowLangue.enable}}</el-radio>
                        <!-- 禁用 -->
                        <el-radio :label="false" v-cloak>{{nowLangue.forbidden}}</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <!--保存-->
                <el-button class="bolder outsideBtnBS" size="mini" type="danger" @click="saveUpdateSpareSubclass" v-text="nowLangue.save" :disabled="saveUSSDisabled"></el-button>
                <!--取消-->
                <el-button class="bolder outsideBtnBS" size="mini" type="danger" @click="cancleUpdateSubclass" v-text="nowLangue.cancel"></el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
       export default {
        data() {
            return {
                nowLangue:{},
                // 查询表单
                subclassForm:{
                    name:"",
                    code:"",
                    status:"",
                    remark:""
                },
                statusLists:[],   //当前状态下拉列表
                labelWidth:"100px",
                addSpareSubclassForm:{
                    name:"",
                    code:"",
                    status:false,
                    remark:""
                },
                updateSpareSubclassForm:{
                    name:"",
                    code:"",
                    status:false,
                    remark:"",
                    version:0  //verion版本控制
                },
                subclassData:[],
                loading:false,
                disabled:false,


                //弹窗
                dialogForm1Visible:false,
                dialogForm2Visible:false,
                // 分页
                isPaging:false,
                pageIndex:1,    //当前页（必传）
                pageSize :10,   //每页多少条
                totalNumber: 20,
                totalPage:2,
                
                //查询禁用
                selectDisabled:false,
                saveSSDisabled:false,    //新增弹窗中保存操作按钮的禁用
                saveUSSDisabled:false   //修改弹窗中保存操作按钮的禁用
            };
        },
        created:function(){
            var _this=this;
            _this.languageType=="zh"?_this.nowLangue=_this.language.zh:_this.nowLangue=_this.language.en;
        },
        computed:{
            // powerData(){
            //     return this.$store.state.powerData;
            // },
            languageType(){
                return this.$store.state.languageType;
            }
        },
        watch:{
            languageType(){
                var _this=this;
                _this.languageType=="zh"?_this.nowLangue=_this.language.zh:_this.nowLangue=_this.language.en;
            }
        },
       //进入页面立即运行此方法
       mounted: function () {
            var _this = this;
            _this.$store.commit('currentActive', 'SpareSubclass');
            // //ajax前后查询按钮禁用
            // $(".selectAll").ajaxStart(function(){
            //     _this.selectDisabled=true;
            // });
            // $(".selectAll").ajaxStop(function(){
            //     _this.selectDisabled=false;
            // });
            _this.dropDownValue();// 当前状态下拉列表
            _this.querySubclssSheet();    //表单查询
       },
        methods:{
            // 当前状态下来列表
            dropDownValue(){
                var _this=this;
                var tmp=["subClassStatus"];
                $.ajax({
                    type:'POST',
                    url:_this.metadata_service+'metadata/getPullDownList',
                    cache:false,
                    data:JSON.stringify(tmp),
                    dataType:'JSON',
                    contentType:"application/json;charset=utf-8",
                    success:function(data){
                        if(data.success){
                            _this.statusLists=data.data;
                        }else{
                            _this.common.errorTip(data.errorMessage);
                        }
                    }
                })
            },
            //查询列表
            querySubclssSheet(){
                var _this=this;
                _this.loading=true;
                var tmp={
                    name:_this.subclassForm.name,
                    code:_this.subclassForm.code,
                    status:_this.subclassForm.status,
                    pageIndex:_this.pageIndex,
                    pageSize:_this.pageSize
                };
                _this.selectDisabled=true;
                $.ajax({
                    type: 'POST',
                    url: _this.metadata_service+"subClass/queryPage",
                    cache: false,
                    data:JSON.stringify(tmp),
                    dataType:'JSON',
                    contentType: "application/json;charset=utf-8",
                    success: function (data) {
                        if (data.success) {
                            _this.loading=false;
                            _this.isPaging=true;
                            _this.totalPage=data.data.totalPage;
                            _this.totalNumber=data.data.totalNumber;
                            _this.subclassData=data.data.dataList;
                        }else{
                            _this.common.errorTip(data.errorMessage)
                        }
                        _this.selectDisabled=false;
                    }
                });
            },
            //新增税码税率信息
            createNew(){
                var _this=this;
                _this.dialogForm1Visible=true; 
                if(this.$refs['addSpareSubclassForm']) {
                    this.$refs['addSpareSubclassForm'].resetFields();
                }
                _this.addSpareSubclassForm.name="";
                _this.addSpareSubclassForm.code="";
                _this.addSpareSubclassForm.status=false;
                _this.addSpareSubclassForm.remark="";
            },
            //新建保存
            saveSpareSubclass(){
                var _this=this;
                _this.$refs["addSpareSubclassForm"].validate((valid) => {
                    if (valid) {
                        var tmp={
                            name:_this.addSpareSubclassForm.name,   //去除税码两边的空格
                            code:_this.addSpareSubclassForm.code,
                            status:_this.addSpareSubclassForm.status,
                            remark:_this.addSpareSubclassForm.remark
                        };
                        _this.saveSSDisabled=true;
                        $.ajax({
                            type: 'POST',
                            url: "http://127.0.0.1:18082/subClass/add",
                            cache: false,
                            data:JSON.stringify(tmp),
                            dataType:'JSON',
                            contentType: "application/json;charset=utf-8",
                            success: function (data) {
                                if (data.success) {
                                    var success=_this.languageType=="zh"?'保存成功':'Save Success';
                                    _this.common.successTip(success);
                                    _this.dialogForm1Visible=false;
                                    _this.querySubclssSheet();
                                }else{
                                    _this.common.errorTip(data.errorMessage);
                                }
                                _this.saveSSDisabled=false;
                            }
                        });
                    } else {
                        var  err=_this.common.formErrorInfo();
                        _this.common.errorTip(err);
                        return false;
                    }
                });
            },
            //新建取消
            cancelSpareSubclass(){
                var _this=this;
                _this.dialogForm1Visible=false;
                if(this.$refs['addSpareSubclassForm']) {
                    this.$refs['addSpareSubclassForm'].resetFields();
                }
                _this.addSpareSubclassForm.name="";
                _this.addSpareSubclassForm.code="";
                _this.addSpareSubclassForm.remark="";
                _this.addSpareSubclassForm.status=false;
                _this.addSpareSubclassForm.remark="";
            },
            // 操作启用禁用
            handleStatus(index, row){
                var _this=this;
                if(row.status){
                    row.status = false;
                }else{
                    row.status = true;
                }
                var tmp={id: row.id,status: row.status}; 
                $.ajax({
                    type: 'POST',
                    url: "http://127.0.0.1:18082/subClass/updateStatus",
                    cache: false,
                    data:JSON.stringify(tmp),
                    dataType:'JSON',
                    contentType: "application/json;charset=utf-8",
                    success: function (data) {
                        if (data.success) {
                            // 当前状态的显示
                            if(row.status){
                                row.statusName = "启用";
                                var success=_this.languageType=="zh"?'启用成功':'Enable success';
                                _this.common.successTip(success);
                            }else{
                                row.statusName = "禁用";
                                var success=_this.languageType=="zh"?'禁用成功':'Disable success';
                                _this.common.successTip(success);
                            }
                        }else
                            _this.common.errorTip(data.errorMessage);
                    }
                });
            },
            //点击修改
            updateCorFact(index,row){
                var _this=this;
                _this.dialogForm2Visible=true;
                if(this.$refs['updateSpareSubclassForm']) {
                    this.$refs['updateSpareSubclassForm'].resetFields();
                 }
                 _this.updateSpareSubclassForm.id = row.id;
                 _this.updateSpareSubclassForm.name = row.name;
                 _this.updateSpareSubclassForm.code = row.code;
                 _this.updateSpareSubclassForm.status = row.status;
                 _this.updateSpareSubclassForm.remark = row.remark;
                 _this.updateSpareSubclassForm.version = row.version;
            },
            //修改保存
            saveUpdateSpareSubclass(){
                var _this=this;
                _this.$refs["updateSpareSubclassForm"].validate((valid) => {
                    if (valid) {
                        var tmp={
                            id:_this.updateSpareSubclassForm.id,
                            name:_this.updateSpareSubclassForm.name,
                            code:_this.updateSpareSubclassForm.code,
                            status: _this.updateSpareSubclassForm.status,
                            remark:_this.updateSpareSubclassForm.remark,
                            version:_this.updateSpareSubclassForm.version//verion版本控制
                        };
                        _this.saveUSSDisabled=true;
                        $.ajax({
                            type: 'POST',
                            url: "http://127.0.0.1:18082/subClass/update",
                            cache: false,
                            data:JSON.stringify(tmp),
                            dataType:'JSON',
                            contentType: "application/json;charset=utf-8",
                            success: function (data) {
                                if (data.success) {
                                    var success=_this.languageType=="zh"?'修改成功！':'Modify successfully';
                                    _this.common.successTip(success);
                                    _this.dialogForm2Visible=false;
                                    _this.querySubclssSheet();
                                }else{
                                    _this.common.errorTip(data.errorMessage);
                                }
                                _this.saveUSSDisabled=false;    
                            }
                        });
                    } else {
                        var  err=_this.common.formErrorInfo();
                        _this.common.errorTip(err);
                        return false;
                    }
                });
            },
            //修改取消
            cancleUpdateSubclass(){
                var _this=this;
                _this.dialogForm2Visible=false;
            },
            //   分页
            handleSizeChange(val) {
                var _this=this;
                _this.pageSize=val;
                _this.querySubclssSheet();

            },
            //点击页码分页查询点击事件
            handleCurrentChange(val) {
                var _this=this;
                _this.pageIndex=val;
                _this.querySubclssSheet();
            }
        }
    }
</script>
